import axios from "axios";
import {
    notification,
    Modal
} from "antd";
const { confirm } = Modal;
// import store from "@/store";
// import {
//     getToken,
//     removeToken,
// } from "@/utils/auth";
// import router from "@/router";

const service = axios.create({
    baseURL: "/",
    timeout: 90000,
});
service.interceptors.request.use(
    (config) => {
        // 是否需要设置 token
        const isToken = (config.headers || {}).isToken === false;
        // if (getToken() && !isToken) {
        //     config.headers["Authorization"] = getToken(); // 让每个请求携带自定义token 请根据实际情况自行修改
        // }
        config.headers["Authorization"] = "o9ELz6uQ09s0yMb1Xyv042zvaQk4";
        return config;
    },
    (error) => {
        Promise.reject(error);
    }
);

// 响应拦截器
service.interceptors.response.use(
    (res) => {
        // 未设置状态码则默认成功状态
        const code = res.data.code || 200;
        // 获取错误信息
        if (code === 401 && res.data.msg === "登录状态已过期") {
            confirm({
                title: '系统提示',
                content: '登录状态已过期，您可以继续留在该页面，或者重新登录',
                okText: "重新登录",
                onOk() {
                    // 普通退出
                    // store.commit("SET_TOKEN", "");
                    // store.commit("SET_PERMISSIONS", []);
                    localStorage.clear();
                    sessionStorage.clear();
                    // removeToken();
                    // router.push("/login");
                },
                onCancel() { },
            });
        } else if (code != "200") {
            notification["error"]({
                message: res.data.msg,
                duration: 3000,
            });
            return Promise.reject(res);
        } else {
            return Promise.resolve(res.data);
        }
    },
    (error) => {
        if (error.response.status === 401) {
            const obj = {
                code: 500,
                msg: "操作权限不足",
            };
            return obj;
        } else {
            notification["error"]({
                message: error.message,
                duration:  5 * 1000,
            });
            return Promise.reject(error);
        }
    }
);
export {
    service as axios
};